
# 想对浮点数执行指定精度的舍入运算
def demo_1():
    print(round(1.23, 1))
    print(round(1.27, 1))
    print(round(-1.27, 1))
    print(round(1.22551, 2))

def demo_2():
    a = 1627731
    print(round(a, -1))
    print(round(a, -2))
    print(round(a, -3))

def demo_3():
    x = 1.2345
    print(format(x, '0.2f'))
    print(format(x, '0.3f'))
    print('value is {0.3f}'.format(x))

# 如果不能允许小误差，考虑使用decimal模块
def demo_4():
    a = 2.1
    b = 4.2
    c = a + b
    print(c)
    c = round(c , 2)
    print(c)

if __name__ == '__main__':
    # demo_1()
    # demo_2()
    # demo_3()
    demo_4()